package com.lyy.client;

import com.lyy.domain.Account;
import com.lyy.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;
import java.util.List;

public class Test1 {

    @Test
    public void testAdd(){
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        IAccountService accountService = (IAccountService) app.getBean("proxyAccountService");
        Account account=new Account();
        account.setMoney(200);
        account.setName("hhh");
        accountService.insert(account);
    }

    @Test
    public void testFindAll(){
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        IAccountService accountService = app.getBean(IAccountService.class);
        List<Account> list = accountService.findAll();
        System.out.println(Arrays.toString(list.toArray()));
    }

    @Test
    public void testFindById(){
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        IAccountService accountService = app.getBean(IAccountService.class);
        Account account = accountService.findById("8e148ef4e524a41af3ce9e551e60b57d");
        System.out.println(account);
    }

    @Test
    public void testDelete(){
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        IAccountService accountService = app.getBean(IAccountService.class);
        accountService.delete("88101cb4972560a13684a4023907f76d");
    }

    /**
     * 测试转账,通过beanFactory来获取增强后的service对象,增加了事务管理的功能
     */
    @Test
    public void testTransfer(){
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过bean的id来获取增强后的accountService对象
        IAccountService accountService = app.getBean(IAccountService.class);
        try {
            String result = accountService.transfer("13b8e5e633908d71d90ea55de3f5e638", "a8af7ca39544044e2850ee82f365723a", 100);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("转账发生了异常");
        }
    }
}
